﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace zytech.common.baseform.sys
{
    public partial class RoleRightEditcs : Form
    {
        protected string SqlConnStr = "";
        protected int state = 0;
        public RoleRightEditcs()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //选择另一个人员时重新加载权限
            treeView1.Nodes.Clear();        //清除树
            InitialFirstNode();             //重新加载树
            treeView1.ExpandAll();          //展开所有节点
            for (int i = 0; i < treeView1.Nodes.Count; i++)
            {
                treeView1.Nodes[i].Checked = false;
            }
            string id = dataGridView1.CurrentRow.Cells["cUserid"].Value.ToString();

            for (int i = 0; i < treeView1.Nodes.Count; i++)
            {
                setright(treeView1.Nodes[0], id);
            }
        }

        #region 加载父节点
        protected virtual void InitialFirstNode()
        {
            string sql = "select * from sys_menu where ParentId = 0";
            DataTable dt = Functions.getDatatable(sql, SqlConnStr);
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode node = new TreeNode();
                node.Tag = dr[0].ToString();        //节点的Tag设置为菜单id
                node.Text = dr[1].ToString();       //节点的Text设置为菜单名

                //加载子节点
                InitialSubNodeList(node);

                treeView1.Nodes.Add(node);
            }
        }
        #endregion

        #region 加载子节点
        protected virtual void InitialSubNodeList(TreeNode node)
        {
            string parentId = node.Tag.ToString();
            string sql = "select * from sys_menu where ParentId=" + parentId;
            DataTable dt = Functions.getDatatable(sql, SqlConnStr);
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode subNode = new TreeNode();
                subNode.Tag = dr[0].ToString();
                subNode.Text = dr[1].ToString();

                InitialSubNodeList(subNode);

                node.Nodes.Add(subNode);
            }
        }
        #endregion

        #region 加载权限
        protected virtual void setright(TreeNode tn, string str)
        {
            //使用递归加载权限
            SetNodeChecked(tn, str);
            foreach (TreeNode tr in tn.Nodes)
            {
                if (tn.Nodes != null && tn.Nodes.Count > 0)
                {
                    setright(tr, str);
                }
            }
        }
        #endregion

        #region 设置权限
        protected virtual void SetNodeChecked(TreeNode tv1, string str)
        {
            state = 0;
            string sql = "select abled from v_menu where cUserid = '" + str + "' and menu_id=" + tv1.Tag;
            SqlConnection conn = new SqlConnection(SqlConnStr);
            SqlCommand com3 = new SqlCommand(sql, conn);
            SqlDataAdapter da3 = new SqlDataAdapter(com3);
            DataSet ds3 = new DataSet();
            da3.Fill(ds3);
            foreach (DataRow row1 in ds3.Tables[0].Rows)
            {
                if (Convert.ToInt32(row1["abled"]) == 1)
                    state = 1;
            }
            if (state == 1)
                tv1.Checked = true;
            else
                tv1.Checked = false;
        }
        #endregion
    }
}
